import type { VbenFormSchema } from '#/adapter/form';
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import type { DescriptionItemSchema } from '#/components/description';

import { h } from 'vue';

import { CommonStatusEnum, DICT_TYPE } from '@vben/constants';
import { getDictOptions } from '@vben/hooks';

import { Tag, Typography } from 'ant-design-vue';

import { z } from '#/adapter/form';
import { getBrandList } from '#/api/ai/llm/brand';
import { DictBadge } from '#/components/dict-badge';
import { Image } from '#/components/image';
import { getFileUrl } from '#/utils/fileUtils';

const { Paragraph } = Typography;
/**
 * 一键快速添加模型表单
 */
export function useQuickFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'brand',
      label: '供应商/平台',
      rules: 'selectRequired',
      component: 'ApiSelect',
      componentProps: {
        api: () => getBrandList(),
        labelField: 'title',
        valueField: 'key',
        placeholder: '请输入供应商/平台',
      },
    },
    {
      fieldName: 'llmApiKey',
      label: 'API Key',
      component: 'Input',
      componentProps: {
        placeholder: '请输入API Key',
      },
    },
  ];
}
/** 表单的字段 */
export function useFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'id',
      component: 'Input',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'icon',
      label: '图标',
      component: 'ImageUpload',
      componentProps: {
        params: {
          fileCategoriesCode: 'XTTB',
          objectId: 'AIMXTB',
          objectType: 'AI模型图标',
        },
      },
      rules: 'required',
    },
    {
      fieldName: 'title',
      label: '名称',
      rules: 'required',
      component: 'Input',
      componentProps: {
        placeholder: '请输入名称',
      },
    },
    {
      fieldName: 'status',
      label: '状态',
      component: 'RadioGroup',
      componentProps: {
        options: getDictOptions(DICT_TYPE.COMMON_STATUS, 'number'),
        buttonStyle: 'solid',
        optionType: 'button',
      },
      rules: z.number().default(CommonStatusEnum.ENABLE),
    },
    {
      fieldName: 'brand',
      label: '供应商/平台',
      rules: 'selectRequired',
      component: 'ApiSelect',
      componentProps(values) {
        return {
          api: () => getBrandList(),
          labelField: 'title',
          valueField: 'key',
          placeholder: '请输入供应商/平台',
          onChange: (value: string, option: any) => {
            values.brandOptions = value ? option.brandOptions || [] : undefined;
            values.llmModel = '';
            values.description = '';
            values.supportChat = false;
            values.supportFunctionCalling = false;
            values.supportMultimodal = false;
            values.supportEmbed = false;
            values.supportOnline = false;
            values.supportThinking = false;
            values.supportReranker = false;
            values.supportTextToImage = false;
            values.supportImageToImage = false;
            values.supportTextToAudio = false;
            values.supportAudioToAudio = false;
            values.supportTextToVideo = false;
            values.supportImageToVideo = false;
            values.llmEndpoint = '';
            values.llmApiKey = '';
            values.llmOptions = { isCustomInput: false };
          },
        };
      },
    },
    {
      fieldName: 'llmModel',
      label: '模型名称',
      rules: 'required',
      component: 'AutoComplete',
      dependencies: {
        triggerFields: ['brand'],
        async componentProps(values) {
          if (!values.brandOptions) {
            return {
              allowClear: true,
              placeholder: '请选择模型名称',
              options: [],
            };
          }
          const filterOption = (input: string, option: any) => {
            return option.value
              .toUpperCase()
              .includes(values.llmModel.toUpperCase());
          };
          const modelList = values.brandOptions.modelList || [];
          const modelOptions = modelList.map((item) => ({
            value: item.llmModel,
            label: item.llmModel,
            ...item,
          }));
          return modelList.length > 0
            ? {
                allowClear: true,
                placeholder: '请选择模型名称',
                options: modelOptions,

                filterOption,
                onSelect(value: string, modelInfo: any) {
                  if (value) {
                    values.description = modelInfo.description || '';
                    values.supportChat = modelInfo.supportChat || false;
                    values.supportFunctionCalling =
                      modelInfo.supportFunctionCalling || false;
                    values.supportMultimodal = modelInfo.multimodal || false;
                    values.supportEmbed = modelInfo.supportEmbed || false;
                    values.supportOnline = modelInfo.supportOnline || false;
                    values.supportThinking = modelInfo.supportThinking || false;
                    values.supportReranker = modelInfo.supportReranker || false;
                    values.supportTextToImage =
                      modelInfo.supportTextToImage || false;
                    values.supportImageToImage =
                      modelInfo.supportImageToImage || false;
                    values.supportTextToAudio =
                      modelInfo.supportTextToAudio || false;
                    values.supportAudioToAudio =
                      modelInfo.supportAudioToAudio || false;
                    values.supportTextToVideo =
                      modelInfo.supportTextToVideo || false;
                    values.supportImageToVideo =
                      modelInfo.supportImageToVideo || false;
                    values.llmEndpoint = values.brandOptions.llmEndpoint || '';
                    values.llmOptions.chatPath =
                      values.brandOptions?.chatPath || '';
                    values.llmOptions.embedPath =
                      values.brandOptions?.embedPath || '';
                    values.llmOptions.rerankPath =
                      values.brandOptions?.rerankPath || '';
                    values.llmOptions.version =
                      values.brandOptions?.version || '';
                  } else {
                    values.description = '';
                    values.supportChat = false;
                    values.supportFunctionCalling = false;
                    values.supportMultimodal = false;
                    values.supportEmbed = false;
                    values.supportOnline = false;
                    values.supportThinking = false;
                    values.supportReranker = false;
                    values.supportTextToImage = false;
                    values.supportImageToImage = false;
                    values.supportTextToAudio = false;
                    values.supportAudioToAudio = false;
                    values.supportTextToVideo = false;
                    values.supportImageToVideo = false;
                    values.llmEndpoint = '';
                    values.llmOptions.chatPath = '';
                    values.llmOptions.embedPath = '';
                    values.llmOptions.rerankPath = '';
                    values.llmOptions.version = '';
                  }
                },
              }
            : {
                allowClear: true,
                placeholder: '请选择模型名称',
                options: [],
              };
        },
      },
    },
    {
      fieldName: 'llmOptions.isCustomInput',
      component: 'Switch',
      componentProps: {
        class: '',
        checkedChildren: '自定义',
        unCheckedChildren: '默认',
        size: 'small',
      },
      defaultValue: false,
    },
    {
      fieldName: 'llmEndpoint',
      label: '请求地址',
      rules: 'required',
      component: 'Input',
      componentProps: {
        placeholder: '请输入名称',
      },
      dependencies: {
        triggerFields: ['llmOptions', 'brand'],
        show: (values) => {
          return (
            (values.llmOptions?.isCustomInput ?? false) ||
            values.brand === 'ollama'
          );
        },
      },
    },
    {
      fieldName: 'llmApiKey',
      label: 'API Key',
      rules: 'required',
      component: 'Input',
      componentProps: {
        placeholder: '请输入API Key',
      },
    },
    {
      fieldName: 'llmOptions.apiSecret',
      label: 'API Secret',
      rules: 'required',
      component: 'Input',
      componentProps: {
        placeholder: '请输入API Secret',
      },
      dependencies: {
        triggerFields: ['brand'],
        show: (values) => values.brand === 'ollama',
      },
    },
    {
      fieldName: 'llmOptions.chatPath',
      label: '对话路径',
      component: 'Input',
      componentProps: {
        placeholder: '请输入对话路径',
      },
      dependencies: {
        triggerFields: ['llmOptions', 'brand'],
        show: (values) => {
          return (
            (values.llmOptions?.isCustomInput ?? false) &&
            values.brand !== 'ollama'
          );
        },
      },
    },
    {
      fieldName: 'llmOptions.embedPath',
      label: '向量化路径',
      component: 'Input',
      componentProps: {
        placeholder: '请输入向量化路径',
      },
      dependencies: {
        triggerFields: ['llmOptions', 'brand'],
        show: (values) => {
          return (
            (values.llmOptions?.isCustomInput ?? false) &&
            values.brand !== 'ollama'
          );
        },
      },
    },
    {
      fieldName: 'llmOptions.rerankPath',
      label: '重排路径',
      component: 'Input',
      componentProps: {
        placeholder: '请输入重排路径',
      },
      dependencies: {
        triggerFields: ['llmOptions', 'brand'],
        show: (values) => {
          return (
            (values.llmOptions?.isCustomInput ?? false) &&
            values.brand !== 'ollama'
          );
        },
      },
    },
    {
      fieldName: 'description',
      label: '模型描述',
      component: 'Textarea',
      componentProps: {
        rows: 4,
      },
    },
    {
      fieldName: 'supportChat',
      label: '对话模型',
      component: 'Switch',
      componentProps: {
        class: '',
        checkedChildren: '支持',
        unCheckedChildren: '不支持',
      },
      defaultValue: true,
      dependencies: {
        triggerFields: ['llmOptions', 'brand'],
        show: (values) => {
          return (
            ((values.llmOptions?.isCustomInput ?? false) && values.brand) ||
            values.brand === 'ollama'
          );
        },
      },
    },
    {
      fieldName: 'supportOnline',
      label: '联网搜索',
      component: 'Switch',
      componentProps: {
        class: '',
        checkedChildren: '支持',
        unCheckedChildren: '不支持',
      },
      defaultValue: false,
      dependencies: {
        triggerFields: ['llmOptions', 'brand'],
        show: (values) => {
          return (
            ((values.llmOptions?.isCustomInput ?? false) && values.brand) ||
            values.brand === 'ollama'
          );
        },
      },
    },
    {
      fieldName: 'supportThinking',
      label: '深度思考',
      component: 'Switch',
      componentProps: {
        class: '',
        checkedChildren: '支持',
        unCheckedChildren: '不支持',
      },
      defaultValue: false,
      dependencies: {
        triggerFields: ['llmOptions', 'brand'],
        show: (values) => {
          return (
            ((values.llmOptions?.isCustomInput ?? false) && values.brand) ||
            values.brand === 'ollama'
          );
        },
      },
    },
    {
      fieldName: 'supportMultimodal',
      label: '多模态',
      component: 'Switch',
      componentProps: {
        class: '',
        checkedChildren: '支持',
        unCheckedChildren: '不支持',
      },
      defaultValue: false,
      dependencies: {
        triggerFields: ['llmOptions', 'brand'],
        show: (values) => {
          return (
            ((values.llmOptions?.isCustomInput ?? false) && values.brand) ||
            values.brand === 'ollama'
          );
        },
      },
    },
    {
      fieldName: 'supportFunctionCalling',
      label: '方法调用',
      component: 'Switch',
      componentProps: {
        class: '',
        checkedChildren: '支持',
        unCheckedChildren: '不支持',
      },
      defaultValue: false,
      dependencies: {
        triggerFields: ['llmOptions', 'brand'],
        show: (values) => {
          return (
            ((values.llmOptions?.isCustomInput ?? false) && values.brand) ||
            values.brand === 'ollama'
          );
        },
      },
    },
    {
      fieldName: 'supportEmbed',
      label: '向量化',
      component: 'Switch',
      componentProps: {
        class: '',
        checkedChildren: '支持',
        unCheckedChildren: '不支持',
      },
      defaultValue: false,
      dependencies: {
        triggerFields: ['llmOptions', 'brand'],
        show: (values) => {
          return (
            ((values.llmOptions?.isCustomInput ?? false) && values.brand) ||
            values.brand === 'ollama'
          );
        },
      },
    },
    {
      fieldName: 'supportReranker',
      label: '重排',
      component: 'Switch',
      componentProps: {
        class: '',
        checkedChildren: '支持',
        unCheckedChildren: '不支持',
      },
      defaultValue: false,
      dependencies: {
        triggerFields: ['llmOptions', 'brand'],
        show: (values) => {
          return (
            ((values.llmOptions?.isCustomInput ?? false) && values.brand) ||
            values.brand === 'ollama'
          );
        },
      },
    },
    {
      fieldName: 'supportTextToImage',
      label: '文生图',
      component: 'Switch',
      componentProps: {
        class: '',
        checkedChildren: '支持',
        unCheckedChildren: '不支持',
      },
      defaultValue: false,
      dependencies: {
        triggerFields: ['llmOptions', 'brand'],
        show: (values) => {
          return (
            ((values.llmOptions?.isCustomInput ?? false) && values.brand) ||
            values.brand === 'ollama'
          );
        },
      },
    },
    {
      fieldName: 'supportImageToImage',
      label: '图生图',
      component: 'Switch',
      componentProps: {
        class: '',
        checkedChildren: '支持',
        unCheckedChildren: '不支持',
      },
      defaultValue: false,
      dependencies: {
        triggerFields: ['llmOptions', 'brand'],
        show: (values) => {
          return (
            ((values.llmOptions?.isCustomInput ?? false) && values.brand) ||
            values.brand === 'ollama'
          );
        },
      },
    },
    {
      fieldName: 'supportTextToAudio',
      label: '文生音频',
      component: 'Switch',
      componentProps: {
        class: '',
        checkedChildren: '支持',
        unCheckedChildren: '不支持',
      },
      defaultValue: false,
      dependencies: {
        triggerFields: ['llmOptions', 'brand'],
        show: (values) => {
          return (
            ((values.llmOptions?.isCustomInput ?? false) && values.brand) ||
            values.brand === 'ollama'
          );
        },
      },
    },
    {
      fieldName: 'supportAudioToAudio',
      label: '音频升音频',
      component: 'Switch',
      componentProps: {
        class: '',
        checkedChildren: '支持',
        unCheckedChildren: '不支持',
      },
      defaultValue: false,
      dependencies: {
        triggerFields: ['llmOptions', 'brand'],
        show: (values) => {
          return (
            ((values.llmOptions?.isCustomInput ?? false) && values.brand) ||
            values.brand === 'ollama'
          );
        },
      },
    },
    {
      fieldName: 'supportTextToVideo',
      label: '文生视频',
      component: 'Switch',
      componentProps: {
        class: '',
        checkedChildren: '支持',
        unCheckedChildren: '不支持',
      },
      defaultValue: false,
      dependencies: {
        triggerFields: ['llmOptions', 'brand'],
        show: (values) => {
          return (
            ((values.llmOptions?.isCustomInput ?? false) && values.brand) ||
            values.brand === 'ollama'
          );
        },
      },
    },
    {
      fieldName: 'supportImageToVideo',
      label: '图升视频',
      component: 'Switch',
      componentProps: {
        class: '',
        checkedChildren: '支持',
        unCheckedChildren: '不支持',
      },
      defaultValue: false,
      dependencies: {
        triggerFields: ['llmOptions', 'brand'],
        show: (values) => {
          return (
            ((values.llmOptions?.isCustomInput ?? false) && values.brand) ||
            values.brand === 'ollama'
          );
        },
      },
    },
    {
      fieldName: 'isVip',
      label: 'vip模型',
      component: 'RadioGroup',
      componentProps: {
        class: '',
        options: [
          {
            label: '普通模型',
            value: false,
          },
          {
            label: 'vip模型',
            value: true,
          },
        ],
        buttonStyle: 'solid',
        optionType: 'button',
      },
      defaultValue: false,
      dependencies: {
        triggerFields: ['llmOptions', 'brand'],
        show: (values) => {
          return (
            values.brand !== null &&
            values.brand !== '' &&
            values.brand !== undefined
          );
        },
      },
    },
  ];
}
/** 详情页字段 */
export function useDetailSchema(): DescriptionItemSchema[] {
  return [
    {
      label: '名称',
      field: 'title',
    },
    {
      label: '供应商/平台',
      field: 'brand',
    },
    {
      label: 'ICON',
      field: 'icon',
      render: (val) => {
        return val
          ? h(Image, {
              src: getFileUrl(val, true),
              width: 60,
              height: 60,
            })
          : '未上传';
      },
    },
    {
      label: '是否vip模型',
      field: 'isVip',
      render: (val) =>
        h(DictBadge, {
          value: val,
        }),
    },

    {
      label: '大模型名称',
      field: 'llmModel',
    },
    {
      label: '大模型请求地址',
      field: 'llmEndpoint',
    },

    {
      label: '大模型 API KEY',
      field: 'llmApiKey',
      render: (val) => h(Paragraph, { copyable: true }, () => val),
    },
    {
      label: '大模型 API SECRET',
      field: 'llmOptions.apiSecret',
      render: (val) => h(Paragraph, { copyable: true }, () => val),
    },
    {
      label: '对话路径',
      field: 'llmOptions.chatPath',
    },
    {
      label: '向量化路径',
      field: 'llmOptions.embedPath',
    },
    {
      label: '重排路径',
      field: 'llmOptions.rerankPath',
      span: 2,
    },
    {
      label: '模型描述',
      field: 'description',
      span: 2,
    },
    {
      label: '模型能力',
      field: 'supportFeatures',
      span: 2,
      render: (val) => {
        return val?.map((item) => {
          return h(Tag, { key: item }, { default: () => item });
        });
      },
    },
  ];
}
/** 列表的搜索表单 */
export function useGridFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'title',
      label: '名称',
      component: 'Input',
      componentProps: {
        placeholder: '请输入名称',
        allowClear: true,
      },
    },
    {
      fieldName: 'status',
      label: '状态',
      component: 'RadioGroup',
      componentProps: {
        options: getDictOptions(DICT_TYPE.COMMON_STATUS, 'number'),
        buttonStyle: 'solid',
        optionType: 'button',
      },
      defaultValue: CommonStatusEnum.ENABLE,
    },
  ];
}

/** 列表的字段 */
export function useGridColumns(): VxeTableGridOptions['columns'] {
  return [
    { type: 'seq', title: '序号', width: 40 },
    {
      field: 'icon',
      title: '图标',
      // cellRender: {name: 'CellImage', }, 不能使用这种方式加载图片，该方式会多次加载
      slots: { default: 'icon' },
    },
    {
      field: 'title',
      minWidth: 150,
      title: '名称',
    },
    {
      field: 'brand',
      title: '供应商',
      minWidth: 180,
      visible: true,
    },
    {
      field: 'llmModel',
      title: '模型名称',
      minWidth: 120,
      visible: true,
    },
    {
      title: '能力',
      field: 'supportFeatures',
      minWidth: 300,
      slots: { default: 'supportFeatures' },
    },
    {
      field: 'description',
      title: '描述',
      minWidth: 300,
    },
    {
      field: 'createTime',
      title: '创建时间',
      minWidth: 180,
      formatter: 'formatDateTime', // 使用Vben Admin内置的日期时间格式化器
    },
    {
      field: 'actions',
      title: '操作',
      width: 200,
      fixed: 'right',
      slots: { default: 'actions' },
    },
  ];
}
